package com.ican.action.user;

import java.util.List;

import javax.annotation.Resource;

import com.ican.domain.user.Resources;
import com.ican.service.user.ResourcesService;
import com.ican.util.ContextUtils;
import com.opensymphony.xwork2.ActionSupport;

public class ResourcesAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    
    private ResourcesService resourcesService;
    
    private List<Resources> resourcesList;
    private Resources resources;
    private String resourceJson;
    private String ajaxPara;
    
    public String list(){
        resourcesList = resourcesService.getAllResource(-1);
        resources2json();
        return "list";
    }

    public void modifyResourceSort(){
        if(ajaxPara == null){
            ContextUtils.write2Response("no");
        }else{
            try{
                String[] params = ajaxPara.split(",");
                Integer fromId = Integer.valueOf(params[0]);
                Integer fromPid = Integer.valueOf(params[1]);
                Integer toId = Integer.valueOf(params[2]);
                Integer toPid = Integer.valueOf(params[3]);
                //"inner"：成为子节点，"prev"：成为同级前一个节点，"next"：成为同级后一个节点
                String moveType = params[4];
                resourcesService.changeResourceSort(fromId, fromPid, toId, toPid, moveType);
                ContextUtils.write2Response("ok");
            }catch(Exception e){
                ContextUtils.write2Response("no");
            }
        }
    }
    
    public String modifyResource(){
        resourcesService.updateResources(resources);
        resources = null;
        return list();
    }
    
    public String addResource(){
        resourcesService.addResources(resources);
        resources = null;
        return list();
    }
    
    private void resources2json(){
        Resources r = null;
        StringBuffer sb = new StringBuffer("[");
        for(int i = 0; i < resourcesList.size(); i++){
            r = resourcesList.get(i);
            sb.append("{id:" + r.getId().toString() + ",");
            if(r.getPid() == null){
                sb.append("pId:0,");
            }else{
                sb.append("pId:" + r.getPid().intValue() + ",");
            }
            sb.append("name:\"" + r.getName() + "\",");
            sb.append("fromAction:\"" + r.getUrl() + "\",");
            sb.append("sort:" + r.getSort() + ",");
            sb.append("dataStatus:" + r.getDataStatus() + "}");
            if(i < resourcesList.size() - 1){
                sb.append(",");
            }
        }
        sb.append("]");
        resourceJson = sb.toString();
    }
    
    @Resource
    public void setResourcesService(ResourcesService resourcesService) {
        this.resourcesService = resourcesService;
    }
    public Resources getResources() {
        return resources;
    }
    public void setResources(Resources resources) {
        this.resources = resources;
    }
    public List<Resources> getResourcesList() {
        return resourcesList;
    }
    public void setResourcesList(List<Resources> resourcesList) {
        this.resourcesList = resourcesList;
    }
    public String getResourceJson() {
        return resourceJson;
    }
    public void setResourceJson(String resourceJson) {
        this.resourceJson = resourceJson;
    }

    public String getAjaxPara() {
        return ajaxPara;
    }

    public void setAjaxPara(String ajaxPara) {
        this.ajaxPara = ajaxPara;
    }
}
